/*
 * DataPackageOps.java
 *
 * Created on June 11, 2007, 11:28 AM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin;

import java.io.File;
import java.util.Calendar;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author demory
 */
public class DataPackageOps {

  private AdminClient av_;

  /** Creates a new instance of ExportOps */
  public DataPackageOps(AdminClient av) {
    av_ = av;
  }

  public void archivePackageToDisk() {
    Calendar now = Calendar.getInstance();
    int day = now.get(Calendar.DAY_OF_MONTH), month = now.get(Calendar.MONTH) + 1, year = now.get(Calendar.YEAR) - 2000; // y21k problem!!
    String date = (month < 10 ? "0" : "") + month + (day < 10 ? "0" : "") + day + (year < 10 ? "0" : "") + year;

    int hour = now.get(Calendar.HOUR_OF_DAY), min = now.get(Calendar.MINUTE);
    String time = (hour < 10 ? "0" : "") + hour + (min < 10 ? "0" : "") + min;

    String defaultFilename = av_.getEngine().getProperty("5pHome") + "archive" + File.separator + av_.getEngine().getDataPackage().getName() + "_" + date + "_" + time + ".fpa";
    //JOptionPane.showMessageDialog(av_.getGUI(), defaultFilename);
    Object[] options = {"Use Default", "Specify Other", "Cancel"};
    int choice = JOptionPane.showOptionDialog(av_.getGUI(), "Default filename for archive:\n" + defaultFilename, "Question", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "Use Default");

    File file = null;
    if (choice == 0) {
      file = new File(defaultFilename);
    } else if (choice == 1) {
      JFileChooser fileChooser = new JFileChooser();
      int result = fileChooser.showOpenDialog(null);
      if (result == JFileChooser.APPROVE_OPTION) {
        file = fileChooser.getSelectedFile();
      }
    }

    if (file != null) {
      av_.getEngine().getDataPackage().writeArchive(file);
      av_.msg("Archive written");
    }
  }

  public void restorePackageFromArchive() {
    int result = JOptionPane.showConfirmDialog(av_.getGUI(), "Archive current state of package first? (Recommended)");
    switch (result) {
      case JOptionPane.YES_OPTION:
        archivePackageToDisk();
        JOptionPane.showMessageDialog(av_.getGUI(), "Select file to restore from");
        break;
      case JOptionPane.NO_OPTION:
        break;
      case JOptionPane.CANCEL_OPTION:
        return;
    }

    JFileChooser fileChooser = new JFileChooser(av_.getEngine().getProperty("5pHome") + "archive");
    result = fileChooser.showOpenDialog(null);
    if (result == JFileChooser.CANCEL_OPTION) {
      return;
    }

    String dir = av_.getEngine().getProperty("5pHome") + "data" + File.separator + av_.getEngine().getDataPackage().getName();
    if (JOptionPane.showConfirmDialog(av_.getGUI(), "This will delete the current contents of\n" + dir, "Confirm", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
      return;
    }

    boolean success = av_.getEngine().getDataPackage().restoreFromArchive(fileChooser.getSelectedFile());

    if (success) {
      JOptionPane.showMessageDialog(av_.getGUI(), "Package successfully restored. The application will now quit. The restored package will load upon restart.");
      System.exit(0);
    }
  }
}
